
<div class="ibox float-e-margins">
    <form class="form-horizontal" id="edit_cost_form">
    <?php foreach ($items as $cost) { ?>
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit</i></h5>
        <div class="ibox-tools">    
             <button class="btn btn-primary btn-sm save-btn-tab" id="submit-uom">Save</button>
             <a id="cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
    <div class="ibox-content">
      <div class="form-group">
          <input type="hidden" name="cost_id" value="<?php echo $cost->cost_id; ?>"> 
          <input type="hidden" name="bu_id" value="<?php echo $cost->bu_id; ?>"> 
          <label class="col-lg-3 control-label">Business Unit</label>
          <div class="col-lg-4">
              <input type="text" class="form-control" value="<?php echo $cost->bu_name; ?>" disabled name="">
          </div>
          <input type="hidden" name="company_id" value="2">
      </div>
      <div class="form-group">
        <label class="col-lg-3 control-label">Item Number</label>
        <div class="col-lg-4">
          <input type="text" style="" name="i_num" class="form-control" value="<?php echo $cost->item_num; ?>" readonly>
        </div>
    </div>
    <div class="form-group"><label class="col-lg-3 control-label">Description</label> 
        <div class="col-lg-4">
            <input type="text"  id="" class="form-control" style="width:238px;" value="<?php echo $cost->description; ?>" readonly>
        </div>
    </div>
    <div class="form-group"><label class="col-lg-3 control-label">Effective Date</label> 
        <div class="col-lg-4">
            <input type="search" value="<?php echo $cost->effective_date; ?>" name="edit_effective_date" id="edit_eff_date" class="form-control" style="width:238px;">
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-3 control-label">Unit Cost</label>
        <div class="col-lg-4">
            <input type="text" style=" text-align: right;" class="form-control" name="edit_cost" value="<?php echo$cost->full_cost; ?>">
        </div>
    </div>
    <?php } ?>
    </form>
</div>
</div>
<script>
    $(document).ready(function(){
        $('#edit_cost_form').data('serialize',$('#edit_cost_form').serialize());
        $(".chosen-select").chosen({width:"150px;"});
        $('#edit_eff_date').datepicker({
                keyboardNavigation: false,
                forceParse: false,
                autoclose: true,
                format: "yyyy-mm-dd"
            });
        $('#edit_cost_form').validate({
            rules: {
                     edit_effective_date: {
                         required: true
                     },
                     edit_cost: {
                         required: true,
                         number: true
                     }
                 },
                 submitHandler: function(form){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/update_cost'); ?>",
                        data: $('#edit_cost_form').serialize(),
                        dataType: 'json',
                        success: function(data){
                            console.log(data);
                            if(data[0].error != 1){
                                notifymsg("success",". . .Inventory Cost Updated Successfully");
                                close_add(); active_search();
                             }else{
                              notifymsg("danger",data[0].msg);
                             }
                           // if(data == "true"){
                           //      notifymsg("success",". . .Inventory Cost Update Successfully");
                           //      close_edit(); active_search();
                           // }else{
                           //  notifymsg("danger","Error, Update Failed");
                           // }
                        },
                        complete: function() {

                        },
                        beforeSend: function(){
                           
                        }
                    });
                 }
        });

        function alert_cancel(){
            if($('#edit_cost_form').serialize()!=$('#edit_cost_form').data('serialize')){
                swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                              close_edit(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        }
        $('#cancel_edit').click(function(){
            alert_cancel();
        });
      $('#editX').click(function(){
            alert_cancel();
      });
    });
</script>